/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMessage;
import java.util.Date;
import java.text.SimpleDateFormat;

import Acceso.Security;
import Modelo.DAL.Guardia_DAO;
import Modelo.Guardia;
import java.io.PrintWriter;
import org.json.JSONObject;

/**
 *
 * @author Jose Leonardo
 */
public class personalAsistAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Llevar Control de Asistencias"};
        if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }

        ActionErrors errors = new ActionErrors();

        registrarAsistenciaForm formBean = (registrarAsistenciaForm) form;
        String fechaGuardia = formBean.getFecha();
        String sede = formBean.getNombreSede();

        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String order = request.getParameter("order");
        String asc = request.getParameter("asc");
        boolean error = false;

        if (fechaGuardia == null || fechaGuardia.equals("")) {
            System.out.println("\n\nFecha de Guardia Requerida");
            errors.add("faltaFecha", new ActionMessage("errors.faltaFecha"));
            saveErrors(request, errors);
            error = true;
        }

        if (sede == null || sede.equals("")) {
            System.out.println("\n\nSede de Guardia Requerida");
            errors.add("falta_Sede", new ActionMessage("errors.falta_Sede"));
            saveErrors(request, errors);
            error = true;
        }

        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        Date fechaG = new Date();
        try {
            fechaG = sdf.parse(fechaGuardia);
        } catch (Exception e) {
            System.out.println("\n\nFecha de Guardia Invalida");
            errors.add("format_Fecha", new ActionMessage("errors.format_Fecha"));
            saveErrors(request, errors);
            error = true;
        }

        if (error) {
            return mapping.findForward(FAILURE);
        }

        System.out.println("La fecha escrita es: " + fechaGuardia);
        Guardia guardia = new Guardia();
        guardia.setFechaGuardia(fechaG);
        guardia.setSede(sede);

        JSONObject data = Guardia_DAO.consultarGuardiasPersonal(guardia, limit, offset, order, asc);
        if (data == null || data.length() <= 0) {
            errors.add("No_hay_Resultados", new ActionMessage("errors.No_hay_Resultados"));
            saveErrors(request, errors);
        }

        data.put("sede", sede);
        data.put("fecha", fechaGuardia);

        response.setContentType("text/json");

        PrintWriter pw = response.getWriter();
        pw.write(data.toString());
        pw.flush();
        pw.close();

        return null;
    }
}
